package com.ruoyi.project.cms.stcal.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.stcal.domain.Stcal;
import com.ruoyi.project.cms.stcal.service.StcalService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/cms/stcal")
public class StcalController extends BaseController {
    //声明业务层属性
    @Autowired
    private StcalService stcalServiceImpl;

    //声明资源跳转的公共前缀
    private String prefix = "cms/stcal";
    //声明单元方法:完成公告类型管理页面跳转
    @RequiresPermissions("cms:stcal:view")
    @GetMapping()
    public String cmsType(){
        return prefix+"/type";
    }
    //声明单元方法：完成公告类型的加载
    @RequiresPermissions("cms:stcal:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Stcal stcal)
    {
        startPage();
        List<Stcal> list = stcalServiceImpl.selectCmsTypeList(stcal);
        return getDataTable(list);
    }
    //新增单元方法:完成新增公告类型页面的转发
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 删除公告类别
     * @param ids
     * @return
     */
    @RequiresPermissions("cms:stcal:remove")
    @Log(title = "浏览统计", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(stcalServiceImpl.deleteTypeByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
    /**
     * 新增保存公告类型
     */
    @RequiresPermissions("cms:stcal:add")
    @Log(title = "浏览", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Stcal stcal)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(stcalServiceImpl.checkTypeNameUnique(stcal)))
        {
            return error("新增浏览'" + stcal.getPpName() + "'失败，浏览人已存在");
        }
        stcal.setPpName(ShiroUtils.getLoginName());
        return toAjax(stcalServiceImpl.insertCmsType(stcal));
    }
    /**
     * 修改浏览次数
     */
    @GetMapping("/edit/{reId}")
    public String edit(@PathVariable("reId") Long reId, ModelMap mmap)
    {
        mmap.put("stcal", stcalServiceImpl.selectTypeById(reId));
        return prefix + "/edit";
    }
    /**
     * 修改浏览人
     */
    @RequiresPermissions("cms:stcal:edit")
    @Log(title = "浏览", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Stcal stcal)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(stcalServiceImpl.checkTypeNameUnique(stcal)))
        {
            return error("修改浏览人'" + stcal.getPpName() + "'失败，浏览人已存在");
        }
        return toAjax(stcalServiceImpl.updateCmsType(stcal));
    }
    /**
     * 校验类型名称
     */
    @PostMapping("/checkTypeNameUnique")
    @ResponseBody
    public String checkPostNameUnique(Stcal stcal)
    {
        return stcalServiceImpl.checkTypeNameUnique(stcal);
    }
}


